//页面加载
var orderMoney = 0,
	resPrice = 0,
	moneyDetail = "";
var flag = "1"; //类型
//初始化 获得地址页 地址信息
//获取用户信息对象
var userInfo = localStorage.getItem("userInfo");
var userInfoObj = JSON.parse(userInfo);
//获取用户地址本地存储信息
var userAddStr = sessionStorage.getItem("userAddMes");
var userAddObj = {},
	QuHuoAddress = "",
	ShouhuoAddress = "";
if (userAddStr) {
	userAddObj = JSON.parse(userAddStr);
	QuHuoAddress = userAddObj.QuHuoAddress;
	ShouhuoAddress = userAddObj.ShouhuoAddress;
	$("#user_tel").val(userAddObj.Shouhuo_Tel);
}
var Desc = $("#product_mes").html();
$("#detail_address").empty();
var $QuHuoAddress = "<p>" + QuHuoAddress + "</p>";
var $ShouhuoAddress = "<p>" + ShouhuoAddress + "</p>";
if (QuHuoAddress != ""&&ShouhuoAddress != "") {
	$("#detail_address").append($QuHuoAddress);
	$("#detail_address").append($ShouhuoAddress);
};


// 2:保温箱
var isKeepWarm = $("#keep_warm").is(':checked');
var Baowenxiang;

function checkKeepWarm() {
	if (isKeepWarm == false) {
		Baowenxiang = 0;
	} else {
		Baowenxiang = 1;
	};
}
checkKeepWarm();
$("#keep_warm").click(function() {
	isKeepWarm = $(this).is(':checked');
	if (isKeepWarm == false) {
		checkKeepWarm();
	} else {
		checkKeepWarm();
	}
});
// 3:易碎物品
var isFragile = $("#fragile").is(':checked');
var fragile;

function checkFragile() {
	if (isFragile == false) {
		fragile = 0;
	} else {
		fragile = 1;
	};
}
checkFragile();
$("#fragile").click(function() {
	isFragile = $(this).is(':checked');
	if (isFragile == false) {
		checkFragile();
	} else {
		checkFragile();
	}
});
// 物品类型
var itemType = "其他";
$("#item_type").html(itemType);
var goodsId = "3";
$("#item_type").on('click', function() {
	$(".mask").css("display", "block");
	$(".itemtype_box").css('display', "block")
})

function itemBoxClick() {
	$(".itemtype_box li .name").off('click').on('click', function() {
		$(".itemtype_box li .name").removeClass('select');
		itemType = $(this).html();
		$(this).addClass('select');
		goodsId = $(this).data("Id");
	})
	$(".itemtype_box .confirm").off('click').on('click', function() {
		$(".mask").css("display", "none");
		$(".itemtype_box").css('display', "none");
		$("#item_type").html(itemType);
	})
	$(".itemtype_box .cancel").off('click').on('click', function() {
		$(".mask").css("display", "none");
		$(".itemtype_box").css('display', "none")
	})
}
itemBoxClick();
// 获取物品类型
$.ajax({
		type: "get",
		data: {
			Types: "1",
		},
		url: currentUrl + "/interfaceDOC/firstProject/User.ashx?action=Get_WuPinHelper",
		dataType: "json",
		success: function(data) {
			if (data.msgcode == "1") {
				console.log(data)
				$(".itemtype_box .type").empty();
				var dataLen = data.data.length;
				var liLen = Math.ceil((dataLen+1) / 3) <= 0 ? 1 : Math.ceil((dataLen+1) / 3)
				for (var k = 0; k < liLen; k++) {
					var liStr = "<li class='flex_row'></li>";
					$(".type").append(liStr);
				};
				for (var i = 0; i < dataLen; i++) {
					var goodsDiv = "";
					var b = Math.ceil((i+1) / 3) <= 0 ? 1 : Math.ceil((i+1) / 3);
					b--;
					goodsDiv += "<div class='name'>" + data.data[i].Name + "</div>";
					$(".type li").eq(b).append(goodsDiv);
					$(".type .name").eq(i).data("Id", data.data[i].Id);
				};
				$(".type li:first div:first").addClass("select");
				itemBoxClick();
			}
		}
	})
	// 获取订单金额
function ajaxOrderMoney() {
	$.ajax({
		type: "post",
		url: currentUrl + "/interfaceDOC/firstProject/User.ashx?action=Get_OrderMoneyOne",
		data: {
			"Quhuo_Lng": userAddObj.Quhuo_Lng,
			"Quhuo_Lat": userAddObj.Quhuo_Lat,
			"Shouhuo_Lng": userAddObj.Shouhuo_Lng,
			"Shouhuo_Lat": userAddObj.Shouhuo_Lat,
			"GoodsMoney": "0",
			"Flag": "1",
			"BaoJiaId": BaoId,
			// "UserId",:"" 同城
		},
		dataType: "json",
		success: function(data) {
			if (data.msg == "成功") {
				orderMoney = data.data.OrderMoney;
				moneyDetail = data.data.moneyDetail;
				$(".money_detail").html(moneyDetail);
				$(".form_submit").html("费用￥" + orderMoney + ",去支付");
			} else {
				return false
			}
		},
		error: function() {
			//console.log("获取订单金额失败");
			return false
		}
	});
}
ajaxOrderMoney();

// 获取保价列表;
$.ajax({
	type: "get",
	url: currentUrl + "/interfaceDOC/firstProject/User.ashx?action=Get_BaoJiaList",
	dataType: "json",
	success: function(data) {
		if (data.msgcode == "1") {
			//console.log(data);
			var dataLen = data.data.length;
			var ulLen = dataLen + 1;
			var liHeight = parseInt($('#keep_num li').css("height"));
			$("#keep_num ul").html("");
			for (var i = 0; i < ulLen; i++) {
				var liStr = "";
				if (i == 0) {
					liStr = "<li class='li_select'><p>" + data.data[i].BaoJiaoMoney + "元</p>";
					liStr += "<p>最高可赔付" + data.data[i].PeiMoney + "元</p></li>"
				} else if (i == dataLen) {
					liStr = "<li></li>"
				} else {
					liStr = "<li><p>" + data.data[i].BaoJiaoMoney + "元</p>";
					liStr += "<p>最高可赔付" + data.data[i].PeiMoney + "元</p></li>"
				}
				$("#keep_num ul").append(liStr);
			};
			//缓存保价ID
			for (var k = 0; k < dataLen; k++) {
				$("#keep_num li").eq(k).data("BaoId", data.data[k].Id);
				$("#keep_num li").eq(k).data("BaoMoney", data.data[k].BaoJiaoMoney);
				$("#keep_num li").eq(k).data("PeiMoney", data.data[k].PeiMoney);
			};
			keepPriceIndex=0;
		}
	},
	error: function() {
		$.alert("无法获得保价列表");
		return false;
	}
});

//保价功能
var myScroll;
var keepPriceIndex = 0;
var BaoMoney = 0,
	PeiMoney = 30,
	IsTouBao = 0,
	BaoId = 6 ;//初始化保价信息 取保价价格0的信息；后台BaoId=6 的保价金额为0;
$("#keep_price").click(function(){
	$(".mask").css("display", "block");
	$(".keep_price_box").css('display', "block");
	var liHeight = parseInt($('#keep_num li').css("height"));
	keepPriceScroll = new IScroll('#keep_num', {
		scrollY: true,
		listenY: true,
		tap: true,
		momentum: false,
		snap: 'li'
	})
	keepPriceScroll.refresh();
	keepPriceScroll.goToPage(0, keepPriceIndex);
	var maxLen = $("#keep_num li").length - 1;
	keepPriceScroll.on('scrollEnd', function() {
		var keepPriceHeight = Math.abs(this.y);
		keepPriceIndex = Math.ceil(keepPriceHeight / liHeight);	//2017.7.3保价计算错误
		if (keepPriceIndex >= maxLen) {
			keepPriceIndex = maxLen - 1;
		}else if(keepPriceIndex<=0){
			keepPriceIndex=0;
		}
		$("#keep_num li").removeClass('li_select').eq(keepPriceIndex).addClass('li_select');
		keepPriceScroll.goToPage(0, keepPriceIndex);
		keepPriceScroll.refresh();
	})
	$('#keep_num li').off("tap").on("tap", function() {
		keepPriceIndex=$(this).index();
		if (keepPriceIndex >= maxLen) {
			keepPriceIndex = maxLen - 1;
		}
		$('#keep_num li').removeClass('li_select').eq(keepPriceIndex).addClass('li_select');
		keepPriceScroll.goToPage(0, keepPriceIndex);
	});
	keepPriceScroll.refresh();
	$(".keep_price_box .confirm").off("click").on("click", function() {
		$(".mask").css("display", "none");
		$(".keep_price_box").css('display', "none");
		BaoMoney = parseInt($("#keep_num .li_select").data("BaoMoney"));
		//console.log(BaoMoney);
		PeiMoney = $("#keep_num .li_select").data("PeiMoney");
		//console.log(PeiMoney);
		BaoId = $("#keep_num .li_select").data("BaoId");
		if (BaoMoney == 0) {
			$(".kprice_num").html("保价");
			$("#keep_price .iconfont").css('visibility', 'hidden');
			$("#keep_price span").css('color', '#ccc');
			IsTouBao = 0;
		} else {
			$(".kprice_num").html("保价" + BaoMoney + "元");
			$("#keep_price .iconfont").css('visibility', 'visible');
			$("#keep_price span").css('color', '#333');
			IsTouBao = 1;
		}
		
		ajaxOrderMoney();
		keepPriceScroll.destroy();
		keepPriceScroll = null;
	})
	$(".keep_price_box .cancel").off("click").on("click", function() {
		$(".mask").css("display", "none");
		$(".keep_price_box").css('display', "none");
		keepPriceScroll.destroy();
		keepPriceScroll = null;
	})
})

//     submit提交  正则检验
$(".form_submit").on('click', function() {
	if(ShouhuoAddress == ""||QuHuoAddress=="") {
		$.alert("请输入地址信息")
		return false;
	} else if (itemType == "") {
		$.alert("请输入地址信息")
		return false;
	} else if ($("#user_tel").val() == "") {
		$.alert("请输入联系号码")
		return false;
	} else {
		Desc = $("#product_mes").val();
		var UserId = userInfoObj.data.UserId;
		var orderData = {
			"Near": "0",
			"Baowenxiang": Baowenxiang,
			"QuHuoAddress": userAddObj.QuHuoAddress,
			"Quhuo_Lng": userAddObj.Quhuo_Lng,
			"Quhuo_Lat": userAddObj.Quhuo_Lat,
			"Quhuo_Person": userAddObj.Quhuo_Person,
			"Quhuo_Tel": userAddObj.Quhuo_Tel,
			"ShouhuoAddress": userAddObj.ShouhuoAddress,
			"Shouhuo_Lng": userAddObj.Shouhuo_Lng,
			"Shouhuo_Lat": userAddObj.Shouhuo_Lat,
			"Shouhuo_Person": userAddObj.Shouhuo_Person,
			"Shouhuo_Tel": userAddObj.Shouhuo_Tel,
			"GoodsId": goodsId,
			"ZhiPrice": "0",
			"WuPin_Money": "0",
			"Desc": Desc,
			"YiSui": fragile,
			"Quhuo_Date": nowTime,
			"YouId": "0",
			"YouMoney": "0",
			"OrderMonery": orderMoney,
			"CityId": "155",
			"UserId": UserId,
			"Types": flag,
			"IsTouBao": IsTouBao,
			"TouMoney": BaoMoney,
			"PeiMoney": PeiMoney,
		}
		var orderData = JSON.stringify(orderData);
		sessionStorage.setItem("orderData", orderData);
		window.location.href = "payment_buy.html";
	}
})